package com.shopping.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;



  public class DB {
	
    public static Connection getConn() {
		Connection conn = null;

		try {
			Class.forName("com.mysql.jdbc.Driver");
			String url = "jdbc:mysql://localhost:3306/mydata?user=root&password=123456";
			conn = DriverManager.getConnection(url);
		}catch(ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		return conn;
    } 
    
    public static void closeConn(Connection conn) {
    	if(conn !=null) {
    		try{
    			conn.close();
    			conn=null;
    		}catch(SQLException e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    public static Statement getStat(Connection conn) {
    	Statement stat = null;
    	try {
			stat = conn.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
    	return stat;
    }
    
    public static void closeStat(Statement stat) {
    	if(stat !=null) {
    		try{
    			stat.close();
    			stat=null;
    		}catch(SQLException e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    public static PreparedStatement getPrepStat(Connection conn,String sql) {
    	PreparedStatement prepStat = null;
    	try {
			prepStat = conn.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
    	return prepStat;
    }
    
    public static void closePrepStat(PreparedStatement prepStat) {
    	if(prepStat !=null) {
    		try{
    			prepStat.close();
    			prepStat=null;
    		}catch(SQLException e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    public static ResultSet getRs(Statement stat,String sql) {
    	ResultSet rs = null;
    	try {
			rs = stat.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
    	return rs;
    }
    
    public static ResultSet getRs(Connection conn,String sql) {
    	ResultSet rs = null;
    	Statement stat= null;
    	try {
    		stat = conn.createStatement();
			rs = stat.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
    	return rs;
    }
    
    public static void closeRs(ResultSet rs) {
    	if(rs !=null) {
    		try{
    			rs.close();
    			rs=null;
    		}catch(SQLException e) {
    			e.printStackTrace();
    		}
    	}
    }
  }
